package TestDemo6;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Test {
    /**
     * @param numbers int整型一维数组
     * @param target  int整型
     * @return int整型一维数组
     */
    public static int[] twoSum(int[] numbers, int target) {
        // write code here
        int[] res = new int[2];
        /**
         key是数组的值，value是数组的下标
         */
        HashMap<Integer, Integer> hash = new HashMap();
        for (int i = 0; i < numbers.length; i++) {
            int temp = target - numbers[i];
            if (hash.containsKey(temp)) {
                //通过数值找下标，再从下标在numbers数组里面找数字
                res[0] = hash.get(temp) + 1;
                res[1] = i + 1;
            } else {
                hash.put(numbers[i], i);
            }
        }
        return res;
    }


    public static int check(HashMap<Integer, Integer> hash, int len) {
        for (Map.Entry<Integer, Integer> entry : hash.entrySet()) {
            if (entry.getValue() + 1 > len / 2) {
                return entry.getKey();
            }
        }
        return -1;
    }

    public static int MoreThanHalfNum_Solution(int[] array) {
        int len = array.length;
        /**
         key--->the number of array
         value---->times of nuber appear
         */
        HashMap<Integer, Integer> hash = new HashMap();
        for (int i = 0; i < len; i++) {
            if (!hash.containsKey(array[i])) {
                hash.put(array[i], 0);
            } else {
                int temp = hash.getOrDefault(array[i], 0);
                hash.put(array[i], ++temp);
            }
            int chk = check(hash, len);
            if (chk != -1) {
                return chk;
            }
        }
        return -1;
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param array int整型一维数组
     * @return int整型一维数组
     */
    public static int[] FindNumsAppearOnce (int[] array) {
        // write code here
        /**
         key--->数组中数字
         value--->出现的次数
         */
        int[] ans=new int[2];
        int index=0;
        HashMap<Integer,Integer> hash=new HashMap();
        for(int i=0;i<array.length;i++){
            if(hash.containsKey(array[i])){
                hash.put(array[i],0);
            }else{
                int temp=hash.getOrDefault(array[i],0);
                hash.put(array[i],++temp);
            }
        }

        for(Map.Entry<Integer,Integer> entry:hash.entrySet()){
            if(entry.getValue() == 1){
                ans[index]=entry.getKey();
                index++;
            }
        }
        return ans;

    }
    public static void main(String[] args) {
    }


}